﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Pets
{    
    public static class TagDecorator
    {
        public static int FindCloseTag(List<string> source, int start, string tag)
        {
            for (int i = start; i < source.Count; i++)
            {
                if (TagDecorator.GetName(source[i]) == "/" + tag)
                {
                    return i;
                }
             
            }
            return -1;
        }

        #region Redact tags

        private const string open_tag_template = "<{0}>";

        private const string close_tag_template = "</{0}>";

        private const string double_tag_template = "<{0}>{1}</{0}>";

        public static string ToOpenTag(string tag)
        {
            return string.Format(open_tag_template, tag);
        }

        public static string ToCloseTag(string tag)
        {
            return string.Format(close_tag_template, tag);
        }

        public static string ToDoubleTag(string tag, string property)
        {
            return string.Format(double_tag_template, new string[] {tag, property});
        }

        #endregion

        #region Получение значений из тегов

        public static string GetName(string source)
        {
            int close = source.IndexOf(">");
            int open = source.IndexOf("/");
            if (open == 2)
            {
                return source.Substring(open + 1, close - open + 1);
            }
            else
            {
                return source.Substring(1, close - 1);
            }
        }

        public static string GetValue(string source)
        {
            const string template = @"<[A-Za-z0-9/][a-zA-Z0-9]+>";
            Regex rgx = new Regex(template);

            return rgx.Replace(source, string.Empty);
        }

        #endregion


    }
}
